<template>
  <div class="index">
    <head-text titleText="智能回答设置"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <Search :searchData="searchData" :searchItem="searchItem"></Search>
        <div>
          <el-button @click="add" plain type="success" size="mini">
            添加智能回答
          </el-button>
        </div>
      </div>
      <div class="table-title">
        <span></span>
        <h5>智能回答列表</h5>
      </div>
      <custom-table
        :tableData="tableData"
        :tableListData="tableListData"
        :tableBtn="tableBtn"
        :currentData="currentData"
        @onPageChange="onPageChange"
        @onSizeChange="onSizeChange"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        v-if="pageReset"
      ></custom-table>
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList"></dialog-com>
  </div>
</template>
<script>
import customTable from "@c/table";
import Search from "@c/search";
import headText from "@c/headText";
import dialogCom from "./dialogCom";
import { getAISettingPageApi, delAISettingApi } from "@s/api";
export default {
  name: "index",
  data() {
    return {
      nowPage: 1,
      total: 0,
      limit: 10,
      pageReset: true,
      ActivityList: [],
      searchItem: [],
      searchData: {
        keyWord: ""
      },
      dialogObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      tableData: [],
      currentData: {}
    };
  },
  components: {
    headText,
    dialogCom,
    Search,
    customTable
  },
  created() {
    // form搜索
    this.searchItem = [
      {
        type: "input",
        label: "关键字：",
        prop: "keyWord",
        placeholder: "请输入关键字"
      }
    ];
    //  table表格
    this.tableListData = [
      { width: "50", label: "", type: "index" },
      { prop: "keyWord", width: "", label: "关键字" },
      { prop: "answer", width: "", type: "html", label: "回答内容", tooltiop: true },
      { prop: "createTime", width: "150", type: "date", label: "创建时间" },
      { label: "操作", type: "btn", width: "180", fixed: "right" }
    ];
    // 按钮
    this.tableBtn = [
      {
        name: "编辑",
        btnType: "primary",
        handleFn: "handleEdit"
      },
      {
        name: "删除",
        btnType: "danger",
        handleFn: "handleDelete"
      }
    ];
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 编辑
    handleEdit(row) {
      this.dialogObj.id = row.id;
      this.dialogObj.show = true;
      this.dialogObj.form = row;
      this.dialogObj.title = "编辑智能回答";
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将删除该智能回答?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let json = [row.id];
        delAISettingApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功！"
          });
        });
      });
    },
    onPageChange(val) {
      this.nowPage = val;
      this.getList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getList();
    },
    // 搜索事件
    getDataList(val) {
      this.searchData = val;
      this.nowPage = 1;
      this.limit = 10;
      this.pageReset = false;
      this.$nextTick(() => {
        this.getList();
        this.pageReset = true;
      });
    },
    //获取列表
    getList() {
      let json = JSON.parse(JSON.stringify(this.searchData));
      json.page = this.nowPage;
      json.limit = this.limit;
      getAISettingPageApi(json).then(res => {
        this.tableData = res.list;
        this.currentData.total = res.total;
      });
    },
    add() {
      this.dialogObj.id = "";
      this.dialogObj.show = true;
      this.dialogObj.title = "添加智能回答";
    },
    table_index(index) {
      return (this.nowPage - 1) * this.limit + index + 1;
    }
  }
};
</script>
<style scoped lang="scss"></style>
